/*
 * QTXpost - a Qt-based blog client.
 * Copyright (C) 2010 Helen Sommer
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 3 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program (in COPYING); if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 * MA  02111-1307  USA
 *
 * The project is hosted on http://code.google.com/p/qtxpost
 */

#ifndef FRIENDS_MANAGER_H
#define FRIENDS_MANAGER_H

#include <QtCore>
#include <QtGui>
#include <QtSql>

#include "ui_FriendsManager.h"
#include "Server.h"
#include "GroupTreeWidgetItem.h"
#include "EditGroup.h"
#include "EditUser.h"
class GroupsTree;
class UsersList;

enum FRIEND_TYPES_LJ {
  T_FRIEND,
  T_MUTUAL,
  T_FRIEND_OF,
  T_ALL
};

enum FRIEND_TYPES_DW {
  T_TRUST_OR_WATCH,
  T_TRUST_AND_WATCH,
  T_TRUST,
  T_TRUSTED,
  T_MUTUAL_TRUST,
  T_WATCH,
  T_WATCHED,
  T_MUTUAL_WATCH,
  T_TRUSTED_WATCHED,
  T_TW_ALL
};

enum EDIT_MODES {
  MODE_USERS,
  MODE_TRUSTGROUPS,
  MODE_CONTENTFILTERS
};

class FriendsManager : public QDialog, private Ui::FriendsManager
{
  Q_OBJECT
    
    public:
  FriendsManager(QWidget *parent = 0);
  QMultiHash <QString, QString> groupsUsers;
  QMultiHash <QString, QString> contentFiltersUsers;
  int currentTab;
  QHash <QString, int> updateUserNames; // mass update: val=0:delete, 1:add
  Server *server;
  EditGroup *editGroup;
  EditUser *editUser;

  void open();
  void initialize(Server *server);
  void updateGroupStart(GroupTreeWidgetItem *item, int isPublic, int sortOrder);
  void deleteGroupStart(GroupTreeWidgetItem *item);
  void updateGroupEnd();
  void addUserGroupStart(QString groupName, QString userName);
  void addUserContentFilterStart(QString groupName, QString userName);
  void deleteUserGroupStart(QString groupName, QString userName, QTreeWidgetItem *selected);
  void updateUserGroupEnd();
  void addFriendEnd();
  void updateUser(QString userName, bool trust, bool watch, QString foreground, QString background);
  void processDropData(QString groupName, QString userName);

 private:
  GroupsTree *groupsTree, *contentFiltersTree;
  UsersList *usersList, *watchedUsersList;
  QGridLayout *friendsLayout;
  QVBoxLayout *groupsLayout, *contentFiltersLayout;
  QButtonGroup *trustButtonGroup, *watchButtonGroup;
  QHash <int, QString> usernames; // row->username
  QHash <QString, int > flags; // username -> trust + watch
  QList <QString> headerUsers, headerCommunities;
  QHash <QString, GroupTreeWidgetItem *> groupItems; //groupName -> group item
  QHash <QString, GroupTreeWidgetItem *> contentFilterItems;
  QMap <int, QString> trustGroups; //groupId -> groupName
  QMap <int, QString> contentFilters; 
  QMap <int, QString> friendTypesLj, friendTypesDw;

  GroupTreeWidgetItem *updatedItem;
  QTreeWidgetItem *selectedItem;
  QCursor currentCursor;
  int gridColumns;
  int serverType;
  bool updated;
  bool initInProgress;
  QString whereFriends;
  QString whereJournals;

  void cleanupFriends();
  void header(QList<QString> labels, int row);
  void initializeHeaders(int type);
  void initializeGroups();
  void initializeContentFilters();
  void initializeFriends();
  bool doUpdateFriends();
  void updateWatchedUsersList();

  private slots:

  void switchTab(int index);
  void updateServer(QString label);
  void switchServer(QString label);
  void saveChanges();
  void updateData();
  void toggleUserTrust(int);
  void toggleUserWatch(int);
  void addStart();
  void addGroupStart();
  void addFriendStart();
  void addContentFilterStart();

  void initializeFriends(int);
  void closeEvent(QCloseEvent *event);
};

#endif
